package com.molyfun.parents.activity.report.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.report.ReportDetailActivity;
import com.molyfun.parents.rest.bean.ReportBriefListResponse;
import com.molyfun.parents.utils.TimeUtils;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 *
 */
public class ReportListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;
    private List<ReportBriefListResponse.ReportBriefBean> mList;

    public ReportListAdapter(Context context) {
        this.mContext = context;
    }

    public ReportListAdapter(Context context, int category) {
        this.mContext = context;
    }

    public void refreshData(List<ReportBriefListResponse.ReportBriefBean> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    public void clearAll(){
        if(mList != null){
            mList.clear();
            notifyDataSetChanged();
        }
    }

    public void addAll(List<ReportBriefListResponse.ReportBriefBean> list) {
        this.mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ReportListViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.layout_report_list_cell, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        final ReportBriefListResponse.ReportBriefBean reportBriefBean = mList.get(position);
        ReportListViewHolder reportListViewHolder = (ReportListViewHolder)holder;
        reportListViewHolder.mTimeInterval.setText(TimeUtils.getDateByTimeMillis(reportBriefBean.getBegindatetime()) + "~" + TimeUtils.getDateByTimeMillis(reportBriefBean.getEnddatetime()));
        reportListViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ReportDetailActivity.startActivity(mContext, reportBriefBean.getId());
            }
        });

    }

    @Override
    public int getItemCount() {
        if (mList == null || mList.size() == 0) {
            return 0;
        }
        return mList.size();
    }

    class ReportListViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.time_interval)
        public TextView mTimeInterval;

        public ReportListViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

}
